home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- Path: tank.news.pipex.net!pipex!warwick!bsmail!talisker!nathan
- From: nathan@pact.srf.ac.uk (Nathan Sidwell)
- Subject: Re: Speed question here...
- Message-ID: <Dn341w.5K3@uns.bris.ac.uk>
- Followup-To: comp.lang.c
- Sender: usenet@uns.bris.ac.uk (Usenet news owner)
- Nntp-Posting-Host: talisker.pact.srf.ac.uk
- Organization: Inmos
- X-Newsreader: TIN [version 1.2 PL2]
- References: <4ftluh$1gkv@hearst.cac.psu.edu> <4g9rbg$3cl@ooze.val.net> <xduenrq418t.fsf@korppi.cs.tut.fi>
- Distribution: inet
- Date: Tue, 20 Feb 1996 17:17:56 GMT
-
- Tero Pulkkinen (p150650@korppi.cs.tut.fi) wrote:
- : >I was curious as to how fast something like the
- : >following would execute:
-
- : > int x;
- : > node *ptr; ptr in linked list
-
- : > for ( ptr = first_node; ptr != NULL; ptr = ptr.next ) {
- : > for ( x = 0; x < max; x++ ) {
- : > array[x] = array_other[x];
- : > }
- : > }
-
- : You should be interested in the assignment, coz its the slowest part of that
- : code. :) If the array item happens to be something else than sizeof 2^n then
- : that assignment requires at least one multiply. :) Slow as hell. better
- : do it this way:
-
- not necessarily true. the compiler could strength reduce a
- multiply by 5 (say) to ((a << 2) + a) for instance. OR the
- compiler could spot that an array is being stepped down, and use
- it's own pointer and increment that. OR it could spot an application
- of memcpy (if it could determine array and array_other are disjoint)
-
- : And btw, if you want fast code, you should use do { } while() -construct
- : instead of for(;;) -loop. That way you avoid having two jumps inside the
- : loop. And if you have situation where you always dont have to execute the
- : insides of the loop, then can do it with an if... Most compilers put the
- : loop test to the start of the loop block, and then do unconditional jump
- : to the start of it. That's one too much. :)
- Compilers generally convert all loop source forms into a
- generic one. In particular for loops are often compiled as,
-
- <init code>
- goto test:
- label:
- <body code>
- <increment code>
- test:
- <test_code>
- jump<cond> label
-
- In a do loop there will be no 'goto test'.
- If the compiler sees that the initialization code sets the conditions
- such that there is at least one loop iteration, it could delete the
- 'goto test' too (I have no idea what the state of the art is on this
- one though).
-
- nathan
- --
- Nathan Sidwell Holder of the Xmris home page
- Chameleon Architecture Group at SGS-Thomson, formerly Inmos
- http://www.pact.srf.ac.uk/~nathan/ Tel 0117 9707182
- nathan@inmos.co.uk or nathan@bristol.st.com or nathan@pact.srf.ac.uk
-